from exts import db
from datetime import datetime

# 用户表
class UserModel(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(100), nullable=False)
    password = db.Column(db.String(200), nullable=False)
    join_time = db.Column(db.DateTime, default=datetime.now)

# 问题表
class QuestionModel(db.Model):
    __tablename__ = "question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(100), nullable=False)
    content = db.Column(db.Text, nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)

    # 外键关系
    author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    author = db.relationship(UserModel, backref="questions")

# 课程表
class Course(db.Model):
    __tablename__ = 'courses'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    pinyin = db.Column(db.String(100))
    image_path = db.Column(db.String(200))
    description = db.Column(db.Text)
    link = db.Column(db.String(200))
    order = db.Column(db.Integer)  # 排序字段
    start_date = db.Column(db.DateTime)

# 课时表
class Lesson(db.Model):
    __tablename__ = 'lessons'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    pinyin = db.Column(db.String(200), nullable=False)
    description = db.Column(db.Text)
    banner_content = db.Column(db.Text)  # 横幅内容

    # 关系定义
    vocabularies = db.relationship('Vocabulary', backref='lesson', lazy=True)
    texts = db.relationship('LessonText', backref='lesson', lazy=True)
    annotations = db.relationship('Annotation', backref='lesson', lazy=True)
    culture_tips = db.relationship('CultureTip', backref='lesson', lazy=True)

# 词汇表
class Vocabulary(db.Model):
    __tablename__ = 'vocabularies'
    id = db.Column(db.Integer, primary_key=True)
    chinese = db.Column(db.String(200))
    pinyin = db.Column(db.String(200))
    english = db.Column(db.Text)
    category = db.Column(db.String(50))  # 分类
    lesson_id = db.Column(db.Integer, db.ForeignKey('lessons.id'))

# 课文内容表
class LessonText(db.Model):
    __tablename__ = 'lesson_texts'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200))
    learning_task = db.Column(db.Text)  # 导学任务
    content = db.Column(db.Text)        # 课文内容
    order_num = db.Column(db.Integer)   # 显示顺序
    lesson_id = db.Column(db.Integer, db.ForeignKey('lessons.id'))

# 注释表
class Annotation(db.Model):
    __tablename__ = 'annotations'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200))
    content = db.Column(db.Text)
    order_num = db.Column(db.Integer)
    lesson_id = db.Column(db.Integer, db.ForeignKey('lessons.id'))

# 文化提示表
class CultureTip(db.Model):
    __tablename__ = 'culture_tips'
    id = db.Column(db.Integer, primary_key=True)
    chinese_title = db.Column(db.String(200))
    chinese_content = db.Column(db.Text)
    english_title = db.Column(db.String(200))
    english_content = db.Column(db.Text)
    lesson_id = db.Column(db.Integer, db.ForeignKey('lessons.id'))